package com.clive.ttwebdemo.service;

import com.clive.ttwebdemo.bean.Dishes;
import com.clive.ttwebdemo.exception.GlobalException;
import com.clive.ttwebdemo.mapper.DishMapper;
import com.clive.ttwebdemo.vo.DishesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DishesServiceImpl implements DishesService{
    @Autowired
    private DishMapper dishMapper;


    public boolean addDishes(Dishes dishes) {
        Date date = new Date();
        dishes.setStatus(1);
        dishes.setBuyconut(0);
        dishes.setCreated(date);
        dishes.setUpdated(date);
        int i = dishMapper.addDishes(dishes);
        if (i <=0) {
            return  false;
        }
        return true;
    }

    @Override
    public Dishes findDishById(Integer id) {
      Dishes dishes = dishMapper.findDishById(id);
        return dishes;
    }

    @Override
    public boolean updateDish(Dishes dishes) {
        if (dishes.getCategory().getId() == null) {
            throw new GlobalException("菜品分类id不能为空");
        }
         Date date = new Date();
        dishes.setUpdated(date);
         int i= dishMapper.updateDish(dishes);
        if (i <=0) {
            return  false;
        }
        return true;
    }

    @Override
    public boolean deleteDish(Integer id) {
        int i = dishMapper.deleteDish(id);
        if (i <=0) {
            return  false;
        }
        return true;
    }

    @Override
    public DishesVo queryDish(Dishes dishes) {
        DishesVo vo = new DishesVo();
        List<Dishes> list = dishMapper.queryDish(dishes);
        vo.setDishess(list);
        int dishesTotal = dishMapper.queryDishCount();
        vo.setDishestotal(dishesTotal);
        return vo;
    }
}
